/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.bpel.generator.init;

import javax.xml.namespace.QName;

import net.yienyien.process.bpel.generator.BPELGenerationException;
import net.yienyien.process.bpel.generator.Contrib;
import net.yienyien.process.bpel.generator.ContribImpl;
import net.yienyien.process.bpel.generator.Production;
import net.yienyien.process.bpel.generator.impl.ProductionImpl;
import net.yienyien.process.pim.ProcessInformation;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.xml.type.util.XMLTypeUtil;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.Input;
import org.eclipse.wst.wsdl.Operation;
import org.eclipse.wst.wsdl.Output;
import org.eclipse.wst.wsdl.PortType;
import org.eclipse.wst.wsdl.WSDLFactory;
import org.open.oasis.docs.wsbpel._2._0.process.abstract_.TBoolean;
import org.open.oasis.docs.wsbpel._2._0.process.abstract_.TProcess;

public class InitContrib extends ContribImpl implements Contrib {

	public static final String KEY="InitContrib";
	
	private ProcessInformation pi;
	private Production prod;
	
	@Override
	public String getContribKey() {
		return KEY;
	}
	
	@Override
	public void prepare(Production data, EObject contribution) {
		pi=(ProcessInformation) contribution;
		prod=data;
	}

	@Override
	public void contrib() throws BPELGenerationException {
		createDefinition(prod, pi.getName(), pi.getTagetNamespace());
		prod.getProcess().setName(pi.getName());
		prod.getProcess().setTargetNamespace(pi.getTagetNamespace());	
	
		prod.getProcess().setExpressionLanguage(pi.getExpressionLanguage());
		prod.getProcess().setQueryLanguage(pi.getQueryLanguage());
		prod.getProcess().setExitOnStandardFault((pi.isExitOnStandardFault())?TBoolean.YES:TBoolean.NO);
	}
	
	private void createDefinition(Production prod, String name, String targetNs) {
		Definition def= WSDLFactory.eINSTANCE.createDefinition();
		
		def.setTargetNamespace(targetNs);
		def.addNamespace("tns", targetNs);
		
		PortType pt=WSDLFactory.eINSTANCE.createPortType();
		
		QName ptQName=(QName) XMLTypeUtil.createQName(targetNs, name+"PortType", "");
		pt.setQName(ptQName);
		
		Operation op=WSDLFactory.eINSTANCE.createOperation();
		op.setName(name+"Op");
		Input input=WSDLFactory.eINSTANCE.createInput();
		Output output=WSDLFactory.eINSTANCE.createOutput();
		op.setInput(input);
		op.setOutput(output);
		pt.getOperations().add(op);
				
		def.addPortType(pt);

		prod.setProcessWSDL(def);
		prod.setTargetOperation(op);
	}



}
